# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=opencolorio
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
pkgver=956.98528dd4
pkgrel=1
pkgdesc="A color management framework for visual effects and animation (mingw-w64)"
arch=('any')
url="https://opencolorio.org/"
license=('BSD')
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-ptex"
         "${MINGW_PACKAGE_PREFIX}-python2"
         "${MINGW_PACKAGE_PREFIX}-tinyxml"
         "${MINGW_PACKAGE_PREFIX}-yaml-cpp")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-openimageio"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-pystring"
             "git")
options=('strip')
source=("${_realname}"::"git+https://github.com/imageworks/OpenColorIO.git"
        #${_realname}-${pkgver}.tar.gz::https://github.com/imageworks/OpenColorIO/tarball/v${pkgver}.tar.gz
        fix-redefinitions.patch
        declspec-typo.patch
        detect-mingw-python.patch
        yaml-api-fix.patch)
sha256sums=('SKIP'
            'c86ad65567f0120a5e0633571f10d5cd116f794b6231c4f15efa23a623f1ee2f'
            '4d0324a241c29f2716131f34fceb865622ec0ab08513e67b9f99d47f54ea8174'
            '3c88098dcc7743d484a4aed514570a7e56e9992aae62f1d60b67652edc67115f'
            'db92a617ab156584be0906139febb4ffd4b032afd5d2482a37b430206b0e7f62')

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd ${_realname}
  patch -p1 -i ${srcdir}/fix-redefinitions.patch
  patch -p1 -i ${srcdir}/declspec-typo.patch
  patch -p1 -i ${srcdir}/detect-mingw-python.patch
  patch -p1 -i ${srcdir}/yaml-api-fix.patch
}

build() {
  local _arch_opt=""
  if [ "${CARCH}" = "i686" ]; then
    _arch_opt="-DOCIO_USE_SSE=OFF"
  fi

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  [[ -d "build-${MINGW_CHOST}" ]] && rm -rf "build-${MINGW_CHOST}"
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
     ${extra_config} \
    -DGLEW_LIBRARIES=${MINGW_PREFIX}/lib/libglew32.dll.a \
    ${_arch_opt} \
    ../${_realname}

  make
}

package() {
  cd build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install
  install -Dm644 ${srcdir}/${_realname}/LICENSE.md ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.md
}
